package com.ruoyi.tranShort.mqtt.request;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.mqtt.api.MqttClientApi;
import com.ruoyi.tranShort.mqtt.CheckUtil;
import com.ruoyi.tranShort.mqtt.ResponseHandle;
import com.ruoyi.tranShort.mqtt.api.GatewayRequest;
import com.ruoyi.tranShort.mqtt.api.GatewayResponse;
import com.ruoyi.tranShort.mqtt.common.constants.ControllerNumberConstants;
import com.ruoyi.tranShort.mqtt.common.constants.TypeConstants;
import com.ruoyi.tranShort.mqtt.entity.ComType;
import com.ruoyi.tranShort.mqtt.entity.Content;
import com.ruoyi.tranShort.mqtt.entity.RequestBase;
import com.ruoyi.tranShort.mqtt.exception.ResponseTimeOutException;
import com.ruoyi.tranShort.mqtt.request.entity.SendMessageData;
import com.ruoyi.tranShort.mqtt.response.TimingResponse;
import com.ruoyi.tranShort.utils.MqttClientUtil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 设置时间
 * @author wwy
 * @date 2021-08-11 14:20
 */
public class TimingRequest implements GatewayRequest {
    private RequestBase requestBase = new RequestBase();
    private ComType comType = new ComType();
    private Content content = new Content();

    public TimingRequest() {
        requestBase.setComType(comType);
        comType.setClientId("");
        comType.setMsgId(System.currentTimeMillis()+"");
        comType.setType(TypeConstants.DOWN);
        comType.setContent(content);
        content.setType(TypeConstants.PASSTHROUGH);
    }

    /**
     * 设置时间
     */
    private Date dateTime;
    private String mac;
    private String pubTopic;
    private int qos;
    @Override
    public TimingResponse execute() {
        TimingResponse timingResponse = new TimingResponse();
        timingResponse.setCode(500);
        if(dateTime != null && StringUtils.isNotEmpty(mac)){
            //时间处理
            String time = (dateTime.getTime()+8*60*60*1000)/1000+"";
            String timeHex = this.timeConvertStringToUTF8(time);
            String message1 = ControllerNumberConstants.SET_TIME+"0400"+timeHex;
            //校验位计算
            String checkStr = CheckUtil.makeChecksum(message1);

            String lastMessage = message1+checkStr+"16";
            SendMessageData sendMessageData = new SendMessageData();
            sendMessageData.setMac(mac);
            sendMessageData.setData(lastMessage);
            content.setData(sendMessageData);
            //发送消息
            MqttClientApi mqttClientApi = MqttClientUtil.getMqttClientApi();
            String message = JSONObject.toJSONString(requestBase);
            mqttClientApi.publishMessage(pubTopic,message,qos);
            try {
                String waitMessage = ResponseHandle.waitMessage(mac);
                //解析响应信息
                String conStr = waitMessage.substring(0, 4);
                if(ControllerNumberConstants.REPLY_SET_TIME.equals(conStr)){
                    //成功
                    timingResponse.setCode(200);
                }
            } catch (ResponseTimeOutException e) {
                timingResponse.setCode(500);
            }
        }

        return timingResponse;
    }

    /**
     * 设置时间
     * @param timeStr
     * @return
     */
    public String timeConvertStringToUTF8(String timeStr){
        long times = Long.parseLong(timeStr);
        String timesStr = Long.toHexString(times);

        //低字节先传
        List<String> timesList = new ArrayList<>();
        int len = timesStr.length();
        int num = 0;
        while (num < len) {
            String s = timesStr.substring(num, num + 2);
            timesList.add(s);
            num = num + 2;
        }
        Collections.reverse(timesList);
        String str = "";
        for (String s : timesList) {
            str +=s;
        }
        return str;
    }

    public Date getDateTime() {
        return dateTime;
    }

    public void setDateTime(Date dateTime) {
        this.dateTime = dateTime;
    }

    public String getMac() {
        return mac;
    }

    public void setMac(String mac) {
        this.mac = mac;
    }

    public String getPubTopic() {
        return pubTopic;
    }

    public void setPubTopic(String pubTopic) {
        this.pubTopic = pubTopic;
    }

    public int getQos() {
        return qos;
    }

    public void setQos(int qos) {
        this.qos = qos;
    }
}
